/*
 * @Author: wangzhongjie
 * @Date: 2020-04-03 14:22:54
 * @LastEditors: wangzhongjie
 * @LastEditTime: 2020-04-03 14:43:22
 * @Description:函数
 * @Email: UvDream@163.com
 */
function add1(x: number, y: number) {
  return x + y;
}
let add2: (x: number, y: number) => number;
type add3 = (x: number, y: number) => number;
interface add4 {
  (x: number, y: number): number;
}
add1(1, 2);
// 可选参数,必选参数不能位于可选参数后。
function add5(x: number, y?: number) {
  return y ? x + y : x;
}
add5(1);
function add6(x: number, y = 0, z: number, q = 1) {
  return x + y + z + q;
}
console.log(add6(1, undefined, 3));

function add7(x: number, ...rest: number[]) {
  return x + rest.reduce((pre, cur) => pre + cur);
}
console.log(add7(1, 2, 3, 4, 5));
// 函数重载
function add8(...rest: number[]): number;
function add8(...rest: string[]): string;
function add8(...rest: any[]): any {
  let first = rest[0];
  if (typeof first === "string") {
    return rest.join("");
  }
  if (typeof first === "number") {
    return rest.reduce((pur, cur) => pur + cur);
  }
}
console.log(add8(1, 2, 3));
console.log(add8("1", "2", "3"));
